/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.taobao.snowsnow.hibernate.dao.ProductCollectDao;
import com.taobao.snowsnow.hibernate.pojo.ProductCollect;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class ProductCollectDaoImpl extends BaseDaoImpl implements ProductCollectDao {
	
	public ProductCollect findById(int id) throws Exception {
		return (ProductCollect) getHibernateTemplate().get(ProductCollect.class, id);
	}
	
	public List<ProductCollect> findByName(String name) throws Exception {
		return super.findByProperty(ProductCollect.class, "name", name);
	}

	public List<ProductCollect> findAll() throws Exception {
		return getHibernateTemplate().find("from ProductCollect");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(ProductCollect.class);
	}

	public List<ProductCollect> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(ProductCollect.class, page, limit);
	}

	@Override
	public ProductCollect getFirstObject() throws Exception {
		Query query = getSession().createQuery("from ProductCollect as o where o.id = (select min(id) from ProductCollect)");
		List<ProductCollect> list = query.list();
		if(list.size() !=0)
			return (ProductCollect) list.get(0);
		else return null;
	}

	@Override
	public ProductCollect getLastObject() throws Exception {
		Query query = getSession().createQuery("from ProductCollect as o where o.id = (select max(id) from ProductCollect)");
		List<ProductCollect> list = query.list();
		if(list.size() !=0)
			return (ProductCollect) list.get(0);
		else return null;
	}

	@Override
	public ProductCollect findByNumIID(Long numIId) throws Exception {
		List<ProductCollect> l =  super.findByProperty(ProductCollect.class, "numIid", numIId);
		return l.size() > 0 ? (ProductCollect) l.get(0) : null;
	}
}
